package com.situ.ssm.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.ssm.dao.BanjiMapper;
import com.situ.ssm.entity.Banji;
import com.situ.ssm.entity.PageBean;
import com.situ.ssm.service.IBanjiService;
import com.situ.ssm.vo.BanjiSearchCondition;
@Service
public class BanjiServiceImpl implements IBanjiService {
	@Autowired
	private BanjiMapper banjiDao;
	
	@Override
	public List<Banji> list() {
		return banjiDao.list();
	}

	@Override
	public PageBean<Map<String, Object>> getPageBean(BanjiSearchCondition condition) {
		PageBean<Map<String, Object>> pageBean = new PageBean<>();
		pageBean.setPageNo(condition.getPageNo());
		pageBean.setPageSize(condition.getPageSize());
		//总的记录数
		Integer totalCount = banjiDao.getTotalCount(condition);
		pageBean.setTotalCount(totalCount);
		//总页数
		Integer totalPage = (int)Math.ceil((double)totalCount/condition.getPageSize());
		pageBean.setTotalPage(totalPage);
		//当前页的数据
		Integer offset = (condition.getPageNo() - 1) * condition.getPageSize();
		List<Map<String, Object>> list =banjiDao.findPageBeanList(condition,offset);
		pageBean.setList(list);
		return pageBean;
	}
	//添加
	@Override
	public boolean insert(Banji banji) {
		Integer count = banjiDao.insert(banji);
		return count == 1 ? true : false;
	}
	//根据id删除
	@Override
	public boolean deleteById(Integer id) {
		Integer count = banjiDao.deleteById(id);
		return count == 1 ? true : false;
	}
	//批量删除
	@Override
	public boolean deleteAll(String[] selectIds) {
		Integer count = banjiDao.deleteAll(selectIds);
		return count > 0 ? true : false;
	}
	//修改前查询
	@Override
	public Banji findById(Integer id) {
		return banjiDao.findById(id);
	}
	//修改
	@Override
	public boolean update(Banji banji) {
		Integer count = banjiDao.update(banji);
		return count == 1 ? true : false ;
	}
	//ajax不重名
	@Override
	public boolean checkName(String name) {
		Integer count = banjiDao.checkName(name);
		return count > 0 ? true : false;
	}

}
